package com.nmu.app.Shrimp.veiw.javagame.util;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public class MouseInput implements MouseListener,MouseMotionListener,MouseWheelListener{
	private static final int BUTTON_COUNT=3;
	private Point mousePos;
	private Point currentPos;
	private Point lastPos;
	private boolean[] mouse;
	private int[] polled;
	private int notches;
	private int polledNotches;
	private boolean dw;
	
	
	//��������ɻ���Եĳ�ʼ��
	public MouseInput() {
		//��ʼ�����λ��
		mousePos=new Point(0, 0);
		//ӛ䛮�ǰ���λ��
		currentPos=new Point(0, 0);
		lastPos=currentPos;
		//ӛ�������¼��Ġ�B
		mouse=new boolean[BUTTON_COUNT];
		//ӛ�������¼���ጷŠ�B//��
		polled=new int[BUTTON_COUNT];
	}
	//?
	public synchronized void poll(){
		mousePos=new Point(currentPos);
		for(int i=0;i<mouse.length;++i){
			if(mouse[i]){
				System.out.println("polled:"+i+":"+polled[i]);
				polled[i]++;
			}else{
				polled[i]=0;
			}
		}
	}
	//�@ȡ���λ��
	public Point getPosition(){
		return mousePos;
	}
	//�@ȡ���c
	public int getNotches(){
		return polledNotches;
	}
	//�Д�ԓ�I�Ƿ���ጷ��б�//?
	public boolean buttonDown(int button){
		return polled[button-1]>0;
		
	}
	public boolean buttonRelease(int button){
		return polled[button-1]==0;
	}
	
	/**dragout�Д෽����ͬ�r����null�c�_�P
	 * 
	 * @param button
	 * @return
	 */
	public boolean allreadyDragedout(int button){
		
		if(polled[button-1]==0&&dw){
			dw=false;
			return true;
		}
		return false;
		
	}
	
	
	//�Д�ԓ�I�Ƿ�//����
	public boolean buttonDownOnce(int button){
		return polled[button-1]==1;
	}
	
	
	
	
	
	
	
	//����ǰλ���c�Ğ�O �¼���˵�λ���c
	public synchronized void mouseMoved(MouseEvent e){
		lastPos=currentPos;
		currentPos=e.getPoint();
		System.out.println(currentPos.getX()+":"+currentPos.getY());
	}
	
	
	
	public void mouseWheelMoved(MouseWheelEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseDragged(MouseEvent e) {
		dw=true;
		currentPos=e.getPoint();
		int button=e.getButton()-1;
		if(button>=0&& button<mouse.length){
			mouse[button]=true;
		}
		System.out.println("x:"+e.getX()+"y:"+e.getY());
	}

	public void mouseClicked(MouseEvent e) {
		mouseMoved(e);
	}

	public void mousePressed(MouseEvent e) {
		int button=e.getButton()-1;
		if(button>=0&& button<mouse.length){
			mouse[button]=true;
			System.out.println("press");
		}
	}

	public void mouseReleased(MouseEvent e) {
		int button=e.getButton()-1;
		if(button>=0&& button<mouse.length){
			mouse[button]=false;
			polled[button]=0;
			System.out.println("realse");
		}		
	}

	public void mouseEntered(MouseEvent e) {
		mouseMoved(e);
	}

	public void mouseExited(MouseEvent e) {
		mouseMoved(e);
	}
	public Point getLastPos() {
		return lastPos;
	}
	public void setLastPos(Point lastPos) {
		this.lastPos = lastPos;
	}

}
